/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package me.oz.test;

import com.donkeylive.qw.po.User;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import me.oz.test.api.IUploadService;
import me.oz.test.api.IUserService;
import me.oz.test.api.ResultMessage;
import me.oz.test.util.GsonUtils;
import me.oz.test.vo.UserVo;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * @author oz
 */
public class Mainer {

    private static final int TIME_OUT = 4;

    private static final String BASE_URL = "http://localhost:8080/DonkeyLive2/v1/";

    private static final String USER_JSON = "{\"userid\":80008031,\"nickname\":\"Zzy\",\"gender\":1,\"registertime\":\"2017-06-30 16:35:14\",\"avatar\":\"http://q.qlogo.cn/qqapp/100371282/56454AFBCB6B9D74655B5B5120A3E777/100\",\"type\":1,\"thirdid\":\"56454AFBCB6B9D74655B5B5120A3E777\"}";

    public static void main(String[] args) {

        OkHttpClient client = new OkHttpClient();

        client.newBuilder().connectTimeout(TIME_OUT, TimeUnit.SECONDS);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        test2(retrofit);

    }

    private final static String UPLOAD_URL = "http://47.93.193.64/upload";

    static void test2(Retrofit retrofit) {

        File file0 = new File("/home/oz/Pictures/timg3.jpeg");

        File file1 = new File("/home/oz/Pictures/timg.jpeg");
        
        File file2 = new File("/home/oz/Pictures/timg2.jpeg");

        final IUploadService service = retrofit.create(IUploadService.class);

        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("type", "2")
                .addFormDataPart("userid", "80008031")
                .addFormDataPart("params", "{'latitude':22.222222,'longitude':114.444444,'status':0,'text':'金木研ALL','title':'东京食种','type':0,'userid':80008031}")
                .addFormDataPart("file0", file0.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file0))
                .addFormDataPart("file1", file1.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file1))
                .addFormDataPart("file2", file2.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file2))
                .build();

        service.upload(requestBody).subscribe(new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable dspsbl) {
          
            
            }

            @Override
            public void onNext(ResponseBody t) {

                try {
                    
                    System.out.println("------------------>>>>> ^_*");
                    
                    System.out.println(t.string());
                    
                    
                } catch (IOException ex) {
                    Logger.getLogger(Mainer.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            @Override
            public void onError(Throwable thrwbl) {
             }

            @Override
            public void onComplete() {
            }

        });

    }

    static void test(Retrofit retrofit) {

        IUserService userService = retrofit.create(IUserService.class);

        UserVo vo = GsonUtils.convertString2Object(USER_JSON, UserVo.class);

        userService.updateUser("80008031", vo).subscribe(new Consumer<ResultMessage>() {
            @Override
            public void accept(ResultMessage t) throws Exception {

                System.out.println(t.toString());

            }

        });

        UserVo vo2 = UserVo.newInstance();

        vo2.setUserid(80008031);

        userService.getUser4(vo2)
                //                .subscribeOn(Schedulers.newThread())
                //                .doOnNext(new Consumer<User>() {
                //                    @Override
                //                    public void accept(User t) throws Exception {
                //
                //                        System.out.println("------------------>>>>> accept");
                //                        
                //                        System.out.println(t.toString());
                //                        
                //                        
                //                    }
                //
                //                })
                .observeOn(Schedulers.io())
                .subscribe(new Observer<User>() {
                    @Override
                    public void onSubscribe(Disposable dspsbl) {

                        System.out.println("------------------>>>>> onSubscribe");

                    }

                    @Override
                    public void onNext(User t) {

                        System.out.println("------------------>>>>> ^_*");

                        System.out.println(t.toString());

                    }

                    @Override
                    public void onError(Throwable thrwbl) {

                        System.out.println("------------------>>>>> onError");

                    }

                    @Override
                    public void onComplete() {

                        System.out.println("------------------>>>>> onComplete");

                    }

                });

    }

}
